﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Hack.IO
{
    /// <summary>
    /// Author: Markus Loibl
    /// Platform: .NET Framework 2.0
    /// Easily create and dispose of temporary files to use in C#.NET applications.
    /// 
    /// 
    /// Example Use
    /// using(TempFile tmp = new TempFile(".png"))
    /// {
    ///     string filename = tmp.FullPath;
    ///     // use the file
    /// }
    /// </summary>
    public class TempFile : IDisposable
    {
        private readonly string _tmpfile;
        private Stream tempStream = null;
        public TempFile()
            : this(string.Empty)
        { }

        public TempFile(string extension)
        {
            _tmpfile = Path.GetTempFileName();
            if (!string.IsNullOrEmpty(extension))
            {
                string newTmpFile = _tmpfile + extension;

                // create tmp-File with new extension ...
                File.Create(newTmpFile, 0);
                // delete old tmp-File
                File.Delete(_tmpfile);

                // use new tmp-File
                _tmpfile = newTmpFile;
            }
        }
        public Stream GetStream(FileMode mode)
        {
            if (tempStream == null)
            {
                tempStream = new System.IO.FileStream(this._tmpfile, mode);
            }
            return tempStream;
        }

        public string FullPath
        {
            get { return _tmpfile; }
        }

        void IDisposable.Dispose()
        {
            if (!string.IsNullOrEmpty(_tmpfile) && File.Exists(_tmpfile))
            {
                if (tempStream != null)
                {
                    tempStream.Flush();
                    tempStream.Close();
                }
                File.Delete(_tmpfile);
            }
        }
    }
}
